<template>
  <div class="home">
    <div class="header">
      <Header></Header>
      <ly-tab
        v-model="selectedId"
        :items="items"
        :options="options"
        @change="getHomeItems"
      >
      </ly-tab>
    </div>

    <section>
      <div v-for="(item, index) in newData" :key="index">
        <Swiper v-if="item.type == 'swiperList'" :dataList="item.data"></Swiper>
        <Icons v-if="item.type == 'iconsList'" :dataList="item.data"> </Icons>
        <Recommend
          v-if="item.type == 'recommendList'"
          :dataList="item.data"
        ></Recommend>
        <Ad v-if="item.type == 'adList'" :dataList="item.data"></Ad>
      </div>
    </section>
    <tab-bar></tab-bar>
  </div>
</template>

<script>
import Header from "../components/home/Header";
import Icons from "../components/home/Icons";
import Swiper from "../components/home/Swiper";
import Recommend from "@/components/home/Recommend";
import Ad from "@/components/home/Ad";
import TabBar from "@/components/common/TabBar";
export default {
  name: "Home",
  components: {
    Header,
    Swiper,
    Icons,
    Recommend,
    Ad,
    TabBar,
  },
  data() {
    return {
      count: 0,
      selectedId: 0,
      items: [],
      newData: [],
      options: {
        activeColor: "#fd7c22",
        // 可在这里指定labelKey为你数据里文字对应的字段名
      },
    };
  },
  methods: {
    // 获取标题栏数据
    getHomeList() {
      this.$http
        .$axios({
          url: "/api",
        })
        .then((res) => {
          this.items = res.data;
        })
        .catch((err) => {
          console.log("请求出错了" + err);
        });
    },

    // 获取每一项的数据
    getHomeItems(item, index) {
      this.$http
        .$axios({
          url: "/api/index?id=" + index,
        })
        .then((res) => {
          // console.log(res);
          this.newData = res.data;
        })
        .catch((err) => {
          console.log("请求出错了" + err);
        });
    },
  },
  beforeCreate() {
    // console.log("beforeCreate");
  },
  created() {
    this.getHomeList();
    this.getHomeItems(null, 0);
  },
  beforeMount() {
    // console.log("beforeMount");
  },
  mounted() {
    // console.log("mounted");
  },
  beforeUpdate() {
    // console.log("beforeUpdate");
  },
  updated() {
    // console.log("updated");
  },
  beforeDestroy() {
    // console.log("beforeDestroy");
  },
  destroyed() {
    // console.log("destroyed");
  },
};
</script>
<style lang="scss" scoped>
// sass的七种数据类型

.home {
  width: 100%;
  height: 100%;
  overflow: scroll;
}
section {
  padding-top: 2.773333rem;
  padding-bottom: 1.76rem;
}

.ly-tabbar {
  background-color: #f2f2f2 !important;
  .ly-tab-item-label {
    font-size: 0.373333rem !important;
  }
}
.header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 99;
}
</style>